local Lua__zhahuan = fk.CreateSkill{
  name = "Lua__zhahuan",
  anim_type = "defensive",
}

Fk:loadTranslationTable{
  ["Lua__zhahuan"] = "炸环",
  [":Lua__zhahuan"] = "当你受到濒死伤害时，你可以弃置任意手牌，令你本局游戏不可使用或弃置牌的点数，然后令伤害来源受到x点伤害（x为你不可使用的点数的数量）。",
  ["@Lua__zhahuan"] = "炸环",
  ["#Lua__zhahuan-discard"] = "炸环：选择要弃置的手牌",
  ["zhahuan_numbers"] = "不可使用的点数",
}

Lua__zhahuan:addEffect(fk.DamageInflicted, {
  can_trigger = function(self, event, target, player, data)
    -- 当玩家自己进入濒死状态且有手牌时可触发
    return target == player and player:hasSkill(Lua__zhahuan.name) 
      and  player.hp <= data.damage and not player:isKongcheng()
  end,
  
  on_cost = function(self, event, target, player, data)
    -- 选择弃置的手牌
    local room = player.room
    local handcards = player:getCardIds("h")
    local to_discard = room:askToDiscard(player, {
        min_num = 1,
        max_num = #handcards,
        cancelable = true,
        include_equip = false,
        prompt = "#Lua__zhahuan-discard",
        skill_name = Lua__zhahuan.name,
    })
    
    if #to_discard > 0 then
      event:setCostData(self, {cards = to_discard})
      return true
    end
    
    return false
  end,
  
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    for i = 1, #cards do
        room:addTableMark(player,"@Lua__zhahuan",Fk:getCardById(cards[i]).number)
    end
    local num = #player:getTableMark("@Lua__zhahuan")
    room:damage {
        from = nil,
        to = data.from,
        damage = num,
        skillName = Lua__zhahuan.name,
      }
  end,
})

-- 禁止使用相同点数的牌
Lua__zhahuan:addEffect("prohibit", {
    prohibit_use = function(self, player, card)
        return player:hasSkill(Lua__zhahuan.name) and 
               table.contains(player:getTableMark("@Lua__zhahuan"), card.number)
      end,
      prohibit_discard = function(self, player, card)
        return player:hasSkill(Lua__zhahuan.name) and 
               table.contains(player:getTableMark("@Lua__zhahuan"), card.number)
      end,

})

return Lua__zhahuan 